using Microsoft.Win32;

namespace Win暂停更新
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = "0";
        }
        string key = @"SOFTWARE\Microsoft\WindowsUpdate\UX\Settings";
        private void button1_Click(object sender, EventArgs e)
        {
            int days = 0;
            try
            {
                days = Convert.ToInt32(textBox1.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确数字！");
                return;
            }

            if (days == 0)
            {
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesStartTime");
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseQualityUpdatesStartTime");
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseUpdatesStartTime");
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesEndTime");
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseQualityUpdatesEndTime");
                WRregedit.DeleteRegistryValue(Registry.LocalMachine, key, "PauseUpdatesExpiryTime");
            }
            else
            {
                string nowIsoStr = DateTime.Now.ToString("yyyy-MM-ddT01:00:00Z");
                string endIsoStr = DateTime.Now.AddDays(days).ToString("yyyy-MM-ddT01:00:00Z");
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesStartTime", nowIsoStr);
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseQualityUpdatesStartTime", nowIsoStr);
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseUpdatesStartTime", nowIsoStr);
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesEndTime", endIsoStr);
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseQualityUpdatesEndTime", endIsoStr);
                WRregedit.WriteRegistryValue(Registry.LocalMachine, key, "PauseUpdatesExpiryTime", endIsoStr);
            }
            ReadDays();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            ReadDays();
        }

        private void ReadDays()
        {
            int days = 0;
            if (!WRregedit.RegistryValueExists(Registry.LocalMachine, key, "PauseFeatureUpdatesEndTime"))
                label1.Text = "系统未设定暂停更新";
            try
            {
                string read = WRregedit.ReadRegistryValue(Registry.LocalMachine, key, "PauseFeatureUpdatesEndTime").ToString();
                DateTime endtime = DateTime.Parse(read);
                days = (endtime - DateTime.Now).Days;
            }
            catch (Exception)
            {
                return;
            }
            label1.Text = $"暂停更新{days}天";
        }



    }
}
